﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRE.Catalog
{
    public partial class FrmDauSach : PRE.Catalog.FrmBase
    {
        #region Variable
        DAL.EntitiesDAL.DauSachDAL _dal = new DAL.EntitiesDAL.DauSachDAL();
        //DAL.EntitiesDAL.TheLoaiDAL _dal2 = new DAL.EntitiesDAL.TheLoaiDAL();
        //DAL.EntitiesDAL.NhaXuatBanDAL _dal3 = new DAL.EntitiesDAL.NhaXuatBanDAL();
        BLL.EntitiesBLL.DauSachBLL _bll = new BLL.EntitiesBLL.DauSachBLL();

        DataTable _dt = new DataTable();
        //DataTable _dt2 = new DataTable();
        //DataTable _dt3 = new DataTable();


        #endregion

        #region Inits
        public FrmDauSach()
        {
            InitializeComponent();
        }

        #region Form Load

        private void FrmDauSach_Load(object sender, EventArgs e)
        {
            PerformRefresh();


        }


        #endregion
        #endregion

        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            DataTable _ma = _dal.Select_Max();
            if (_ma != null)
                if (_ma.Rows.Count > 0)
                {
                    string ma = _bll.AddID(_ma.Rows[0]["Ma"] + "");
                    txt_MaDauSach.Text = ma;

                    ReadOnlyControl(false);
                    ClearDataBindings();
                    ChangeStatus(false);

                    cbo_MaTheLoai.ResetText();
                    cbo_MaNhaXuatBan.ResetText();

                    txt_MaDauSach.Properties.ReadOnly = true;

                    txt_tentheloai.Properties.ReadOnly = true;
                    txt_tennhaxuatban.Properties.ReadOnly = true;

                }

            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            var id1 = gridView_DauSach.GetFocusedRowCellValue("MADAUSACH") + "";

            DialogResult result;
            var ok = _dal.Delete(id1);
            result = XtraMessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    XtraMessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            txt_MaDauSach.Properties.ReadOnly = true;
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            ResetText();
            txt_tentheloai.Properties.ReadOnly = true;
            txt_tennhaxuatban.Properties.ReadOnly = true;
            ckeSearch_TheLoai.Visible = false;
            ckeSearch_NhaXuatBan.Visible = false;

            btnGoSearch.Visible = false;

            if (_dt != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dt.Rows.Count > 0) DataBindingControl();
            }



            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            if (IsAdd)
            {
                var o = new DAL.Entities.DauSach()
                {
                    MADAUSACH= Convert.ToString(txt_MaDauSach.Text),
                    TENDAUSACH = Convert.ToString(txt_TenDauSach.Text),
                    NOIDUNG_TT = Convert.ToString(txt_NoiDungTomTat.Text),
                    NGONNGU = Convert.ToString(txt_NgonNgu.Text),
                    NAMXUATBAN = Convert.ToDateTime(dte_NamXuatBan.Text),
                    LANXUATBAN = Convert.ToInt32(txt_LanXuatBan.Text),
                    KICHTHUOC = Convert.ToString(txt_KichThuoc.Text),
                    SOTRANG = Convert.ToInt32(txt_SoTrang.Text),
                    MATHELOAI = Convert.ToString(cbo_MaTheLoai.EditValue),
                    MANHAXB = Convert.ToString(cbo_MaNhaXuatBan.EditValue)
                  
                    

                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    XtraMessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                    PerformAdd();
                }
                else
                {
                    XtraMessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            if (IsEdit)
            {
                var id1 = gridView_DauSach.GetFocusedRowCellValue("MADAUSACH") + "";
                //var id2 = gridView_TacGia.GetFocusedRowCellValue("MalopID") + "";

                var o = new DAL.Entities.DauSach()
                {
                    MADAUSACH = Convert.ToString(txt_MaDauSach.Text),
                    TENDAUSACH = Convert.ToString(txt_TenDauSach.Text),
                    NOIDUNG_TT = Convert.ToString(txt_NoiDungTomTat.Text),
                    NGONNGU = Convert.ToString(txt_NgonNgu.Text),
                    NAMXUATBAN = Convert.ToDateTime(dte_NamXuatBan.Text),
                    LANXUATBAN = Convert.ToInt32(txt_LanXuatBan.Text),
                    KICHTHUOC = Convert.ToString(txt_KichThuoc.Text),
                    SOTRANG = Convert.ToInt32(txt_SoTrang.Text),
                    MATHELOAI = Convert.ToString(cbo_MaTheLoai.EditValue),
                    MANHAXB = Convert.ToString(cbo_MaNhaXuatBan.EditValue)
                };

                var oki = _dal.Update(o);

                if (oki)
                {
                    XtraMessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    XtraMessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            txt_MaDauSach.ResetText();
            txt_TenDauSach.ResetText();
            txt_NoiDungTomTat.ResetText();
            txt_NgonNgu.ResetText();
            dte_NamXuatBan.ResetText();
            txt_LanXuatBan.ResetText();
            txt_KichThuoc.ResetText();
            txt_SoTrang.ResetText();
            cbo_MaTheLoai.ResetText();
            txt_tentheloai.ResetText();
            cbo_MaNhaXuatBan.ResetText();
            txt_tennhaxuatban.ResetText();

            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txt_MaDauSach.DataBindings.Clear();
            txt_TenDauSach.DataBindings.Clear();
            txt_NoiDungTomTat.DataBindings.Clear();
            txt_NgonNgu.DataBindings.Clear();
            dte_NamXuatBan.DataBindings.Clear();
            txt_LanXuatBan.DataBindings.Clear();
            txt_KichThuoc.DataBindings.Clear();
            txt_SoTrang.DataBindings.Clear();
            cbo_MaTheLoai.DataBindings.Clear();
            txt_tentheloai.DataBindings.Clear();
            cbo_MaNhaXuatBan.DataBindings.Clear();
            txt_tennhaxuatban.DataBindings.Clear();


            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {

            txt_MaDauSach.DataBindings.Add("Text", _dt, ".MADAUSACH");
            txt_TenDauSach.DataBindings.Add("Text", _dt, ".TENDAUSACH");
            txt_NoiDungTomTat.DataBindings.Add("Text", _dt, ".NOIDUNG_TT");
            txt_NgonNgu.DataBindings.Add("Text", _dt, ".NGONNGU");
            dte_NamXuatBan.DataBindings.Add("Text", _dt, ".NAMXUATBAN");
            txt_LanXuatBan.DataBindings.Add("Text", _dt, ".LANXUATBAN");
            txt_KichThuoc.DataBindings.Add("Text", _dt, ".KICHTHUOC");
            txt_SoTrang.DataBindings.Add("Text", _dt, ".SOTRANG");
            cbo_MaTheLoai.DataBindings.Add("EditValue", _dt, ".MATHELOAI");
            txt_tentheloai.DataBindings.Add("Text", _dt, ".TENTHELOAI");
            cbo_MaNhaXuatBan.DataBindings.Add("EditValue", _dt, ".MANHAXB");
            txt_tennhaxuatban.DataBindings.Add("Text", _dt, ".TENNHAXB");

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
           
            txt_MaDauSach.Properties.ReadOnly = isReadOnly;
            txt_TenDauSach.Properties.ReadOnly = isReadOnly;
            txt_NoiDungTomTat.Properties.ReadOnly = isReadOnly;
            txt_NgonNgu.Properties.ReadOnly = isReadOnly;
            dte_NamXuatBan.Properties.ReadOnly = isReadOnly;
            txt_LanXuatBan.Properties.ReadOnly = isReadOnly;
            txt_KichThuoc.Properties.ReadOnly = isReadOnly;
            txt_SoTrang.Properties.ReadOnly = isReadOnly;
            cbo_MaTheLoai.Properties.ReadOnly = isReadOnly;
            cbo_MaNhaXuatBan.Properties.ReadOnly = isReadOnly;

            gridControl_DauSach.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            ChangeStatus(false);
            ReadOnlyControl(false);
            ResetText();

            ckeSearch_TheLoai.Visible = true;
            ckeSearch_NhaXuatBan.Visible =true;

            btnGoSearch.Visible = true;

            gridControl_DauSach.Enabled = true;

            base.PerformSearch();
        }

        /// <summary>
        /// Execute Search data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnGoSearch_Click(object sender, EventArgs e)
        {
            try
            {
                int check = 0;
                if (ckeSearch_TheLoai.Checked == true)
                    check = 0;
                if (ckeSearch_NhaXuatBan.Checked == true)
                    check = 1;

                switch (check)
                {
                    case 0:
                        string theloai = cbo_MaTheLoai.EditValue.ToString();
                        gridControl_DauSach.DataSource = _dal.Search(theloai);
                        break;
                    case 1:
                        string nhaxuatban = cbo_MaNhaXuatBan.EditValue.ToString();
                        gridControl_DauSach.DataSource = _dal.Search_NXB(nhaxuatban);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {

                XtraMessageBox.Show("Lỗi! Không lọc được dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
           

           
        }
        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            _dt = _dal.Select();
         
            cbo_MaTheLoai.Properties.DataSource = _dt;
            cbo_MaNhaXuatBan.Properties.DataSource = _dt;
            try
            {
                if (_dt != null)
                {
                    gridControl_DauSach.DataSource = _dt;

                    gridView_DauSach.OptionsBehavior.ReadOnly = true;
                    gridView_DauSach.OptionsView.ColumnAutoWidth = true;
                }

                base.LoadData();
            }
            catch (Exception ex)
            {

                XtraMessageBox.Show("Lỗi! Không load được dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        #endregion

        #region events

        #region private void ckeSearch_TheLoai_CheckedChanged(object sender, EventArgs e)
        private void ckeSearch_TheLoai_CheckedChanged(object sender, EventArgs e)
        {
            if (ckeSearch_TheLoai.Checked == true)
            {
                cbo_MaTheLoai.Properties.ReadOnly = false;

                txt_MaDauSach.Properties.ReadOnly = true;
                txt_TenDauSach.Properties.ReadOnly = true;
                txt_NoiDungTomTat.Properties.ReadOnly = true;
                txt_NgonNgu.Properties.ReadOnly = true;
                dte_NamXuatBan.Properties.ReadOnly = true;
                txt_LanXuatBan.Properties.ReadOnly = true;
                txt_KichThuoc.Properties.ReadOnly = true;
                txt_SoTrang.Properties.ReadOnly = true;
                cbo_MaNhaXuatBan.Properties.ReadOnly = true;
            }
            else
            {
                cbo_MaTheLoai.Properties.ReadOnly = true;

                txt_MaDauSach.Properties.ReadOnly = false;
                txt_TenDauSach.Properties.ReadOnly = false;
                txt_NoiDungTomTat.Properties.ReadOnly = false;
                txt_NgonNgu.Properties.ReadOnly = false;
                dte_NamXuatBan.Properties.ReadOnly = false;
                txt_LanXuatBan.Properties.ReadOnly = false;
                txt_KichThuoc.Properties.ReadOnly = false;
                txt_SoTrang.Properties.ReadOnly = false;
                cbo_MaNhaXuatBan.Properties.ReadOnly = false;

                txt_MaDauSach.Text = null;
                txt_MaDauSach.Focus();
            }
        }

        #endregion

        #region private void ckeSearch_NhaXuatBan_CheckedChanged(object sender, EventArgs e)
        private void ckeSearch_NhaXuatBan_CheckedChanged(object sender, EventArgs e)
        {
            if (ckeSearch_NhaXuatBan.Checked == true)
            {
                cbo_MaNhaXuatBan.Properties.ReadOnly = false;

                txt_MaDauSach.Properties.ReadOnly = true;
                txt_TenDauSach.Properties.ReadOnly = true;
                txt_NoiDungTomTat.Properties.ReadOnly = true;
                txt_NgonNgu.Properties.ReadOnly = true;
                dte_NamXuatBan.Properties.ReadOnly = true;
                txt_LanXuatBan.Properties.ReadOnly = true;
                txt_KichThuoc.Properties.ReadOnly = true;
                txt_SoTrang.Properties.ReadOnly = true;
                cbo_MaTheLoai.Properties.ReadOnly = true;
            }
            else
            {
                cbo_MaNhaXuatBan.Properties.ReadOnly = true;

                txt_MaDauSach.Properties.ReadOnly = false;
                txt_TenDauSach.Properties.ReadOnly = false;
                txt_NoiDungTomTat.Properties.ReadOnly = false;
                txt_NgonNgu.Properties.ReadOnly = false;
                dte_NamXuatBan.Properties.ReadOnly = false;
                txt_LanXuatBan.Properties.ReadOnly = false;
                txt_KichThuoc.Properties.ReadOnly = false;
                txt_SoTrang.Properties.ReadOnly = false;
                cbo_MaTheLoai.Properties.ReadOnly = false;

                txt_MaDauSach.Text = null;
                txt_MaDauSach.Focus();
            }
        }
        #endregion     

       
        #endregion

       





    }
}
